package cn.hn.java.summer.nap;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import cn.hn.java.summer.db.SnJdbcTemplate;
import cn.hn.java.summer.db.multiple.MultipleDataSource;
import cn.hn.java.summer.db.paging.MysqlPagingConverter;

@EnableAutoConfiguration(exclude = {ErrorMvcAutoConfiguration.class})
@SpringBootApplication(scanBasePackages="cn.hn.java.summer.nap")
@Configuration()
@EnableTransactionManagement
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class App
{
	@Autowired
	Config config;
	
    public static void main( String[] args ) throws URISyntaxException, IOException
    {
    	SpringApplication.run(App.class);
    }
    
    @Bean
    public Config getConfig(
		@Value("${nap.dbName}") String dbName, 
		@Value("${nap.package}") String nameSpace,
		@Value("${nap.tablePrefix}") String tablePrefix
    ){
    	return new Config(dbName,nameSpace,tablePrefix);
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource")
    public DataSource dataSource() {
        return new BasicDataSource();
    }
 
    @Bean
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public CacheManager cacheManager() {
    	SimpleCacheManager cache=new SimpleCacheManager();
    	List<Cache> caches=new ArrayList<Cache>();
    	caches.add(new ConcurrentMapCache("mainCache", true));
    	cache.setCaches(caches);
    	return cache;
    }
    
    @Bean
    public MultipleDataSource multipleDataSource(){
    	MultipleDataSource dataSource=new MultipleDataSource();
    	List<SnJdbcTemplate> templates=new ArrayList<SnJdbcTemplate>();
    	SnJdbcTemplate template=new SnJdbcTemplate();
    	template.setDataSource(dataSource());
    	template.setPagingConverter(new MysqlPagingConverter());
    	templates.add(template);
    	dataSource.setJdbcTemplates(templates);
    	return dataSource;
    }
    
}